/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-10-07 13:25:34
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-10-07 14:12:27
 * @FilePath: /sjf/2024-project/2024_centos/test_to_c/sql_ha_test/list_test.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "list_test.h"

seq_node* sql_init()
{
    seq_node* newnode = (STLDataType*)malloc(sizeof(STLDataType));
    newnode->_data = 0;
    newnode->_next = NULL;
    return newnode;
}

void sql_print(seq_node* head)
{
    seq_node* cur = head;
    while(cur != NULL)
    {
        printf("%d",cur->_data);
        cur = cur->_next;
    }
    printf("\n");
}

seq_node* newnode_list(STLDataType x)
{
    seq_node* new_node = (STLDataType*)malloc(sizeof(STLDataType));
    new_node->_data = x;
    new_node->_next = NULL;
    return new_node;
}

//在pos节点之前插入节点
/**
 * node1  pos node2
 * node1 newnode pos node2
 */
void insert_to_left(seq_node**head,seq_node* pos,STLDataType x)
{
    seq_node* newnode = newnode_list(x);
    //表示的是在头插
    if(pos == *head)
    {
        newnode->_next = pos;
        *head = newnode;
    }
    else
    {
        seq_node* cur = *head;
        while(cur->_next != pos)
            cur = cur->_next;
        newnode->_next = cur->_next;
        cur->_next = newnode;
    }
}

//在pos节点之后插入节点
/**
 * node1 pos node2
 * node1 pos newnode node2
 */
void insert_to_right(seq_node** head,seq_node* pos,STLDataType x)
{
    seq_node* newnode  = newnode_list(x);
    seq_node* cur = *head;
    while(cur->_next != pos)
        cur = cur->_next;
    newnode->_next = cur->_next->_next;
    pos->_next = newnode;
}

//删除给定位置的节点
void erase(seq_node** head,seq_node* pos)
{
    assert(pos);
    if(pos == *head)
    {
        *head = pos->_next;
        free(pos);
        pos= NULL;
    }
    else
    {
        seq_node* cur = *head;
        while(cur->_next != pos)
            cur = cur->_next;
        cur->_next = pos->_next;
        free(pos);
        pos = NULL;
    }
}

